﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HotelManagement.Common;
using HotelManagement.BL;

namespace HotelManagement.PL
{
    public partial class SearchGoodsTypeForm : Form
    {
        public SearchGoodsTypeForm()
        {
            InitializeComponent();
            UpdateGridView();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            UpdateGridView();
        }

        internal void UpdateGridView()
        {
            int nResult;
            try
            {
                Goods goods = new Goods();
                goods.Name = txtName.Text.Trim();

                GoodsBL searchBL = new GoodsBL();
                nResult = searchBL.Search(ref goods);

                if (goods.GoodsCollection != null)
                {
                    dgvGoods.AutoGenerateColumns = false;
                    dgvGoods.DataSource = goods.GoodsCollection;
                }
            }
            catch
            {

            }
        }

        private int CheckChoose()
        {
            int nCount = 0;
            int nRowIndex = -1;
            foreach (DataGridViewRow dgr in dgvGoods.Rows)
            {
                if (dgr.Cells["choose"].Value != null && dgr.Cells["choose"].Value.Equals(true))
                {
                    nCount++;
                    nRowIndex = dgr.Index;
                }
            }

            if (nCount == 0)
            {
                MessageBox.Show("请选择货物", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                nRowIndex = -1;
            }

            if (nCount > 1)
            {
                MessageBox.Show("只能选择一个", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                nRowIndex = -1;
            }

            return nRowIndex;
        }

        private Goods buildGoods(int rowIndex)
        {
            Goods goods = new Goods();
            goods.ID = Convert.ToInt32(dgvGoods.Rows[rowIndex].Cells["ID"].Value);
            goods.Name = dgvGoods.Rows[rowIndex].Cells["NAME"].Value.ToString();
            goods.PurchasePrice = Convert.ToSingle(dgvGoods.Rows[rowIndex].Cells["PURCHASEPRICE"].Value);
            goods.SalePrice = Convert.ToSingle(dgvGoods.Rows[rowIndex].Cells["SALEPRICE"].Value);
            goods.WaringCount = Convert.ToInt32(dgvGoods.Rows[rowIndex].Cells["WARINGCOUNT"].Value);
            goods.Description = dgvGoods.Rows[rowIndex].Cells["DESCRIPTION"].Value.ToString();
            return goods;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int nSelectRow = CheckChoose();
            if (nSelectRow < 0)
            {
                return;
            }
            GoodsMngForm goodsMngForm = new GoodsMngForm(true);
            goodsMngForm.GoodsInfo = buildGoods(nSelectRow);
            goodsMngForm.ShowDialog(this);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            GoodsMngForm goodsMngForm = new GoodsMngForm(false);
            goodsMngForm.ShowDialog(this);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int nSelectRow = CheckChoose();
            if (nSelectRow < 0)
            {
                return;
            }

            if (DeleteGoods(buildGoods(nSelectRow)))
            {
                MessageBox.Show("操作成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                UpdateGridView();
            }
            else 
            {
                MessageBox.Show("操作失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private bool DeleteGoods(Goods goods) 
        {
            bool bReturn = false;
            GoodsMngBL goodsMng = new GoodsMngBL();
            try
            {
                bReturn = goodsMng.DeleteGoods(goods) > 0 ? true: false;
            }
            catch
            {

            }

            return bReturn;
        }
    }
}
